package com.tansun.easycare.rule.deployonline.entity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.drools.core.util.StringUtils;

import com.tansun.easycare.rule.datamodel.entity.RuleDataparams;
import com.tansun.easycare.rule.datamodel.entity.RuleDataset;
import com.tansun.easycare.rule.referencedata.entity.RuleReferenceData;
import com.tansun.easycare.rule.ruleversion.entity.RuleVersioninfo;
import com.tansun.ruledata.data.entity.DeployDataparams;
import com.tansun.ruledata.data.entity.DeployDataset;
import com.tansun.ruledata.data.entity.DeployEnterpoint;
import com.tansun.ruledata.data.entity.DeployReferenceData;

/**
 * 规则发布Entity
 * @version 2017-04-11
 */
public class RuleDataInfo implements Serializable {

	private RuleDeployment deployment;
	private List<RuleEnterpoint> enterPoints;
	private List<RuleVersioninfo> flowVersions;
	private List<RuleVersioninfo> refModelList;
	private List<RuleDataset> dataSetlist;
	private List<RuleDataparams> paraList;
	private List<RuleReferenceData> refList;
	
	public com.tansun.ruledata.entity.RuleDataInfo changeClass(){
		com.tansun.ruledata.entity.RuleDataInfo newinfo = new com.tansun.ruledata.entity.RuleDataInfo();
		if(deployment!=null)newinfo.setDeployment(deployment.changeClass());
		if(enterPoints!=null && !enterPoints.isEmpty()){
			List<DeployEnterpoint> newList = new ArrayList<DeployEnterpoint>();
			enterPoints.stream().forEach(item->{ newList.add(item.changeClass()); });
			newinfo.setEnterPoints(newList);
		}
		if(flowVersions!=null && !flowVersions.isEmpty()){
			List<com.tansun.ruledata.entity.RuleVersioninfo> newList = new ArrayList<com.tansun.ruledata.entity.RuleVersioninfo>();
			flowVersions.stream().forEach(item->{ newList.add(item.changeClass()); });
			newinfo.setFlowVersions(newList);
		}
		if(dataSetlist!=null && !dataSetlist.isEmpty()){
			List<DeployDataset> newList = new ArrayList<DeployDataset>();
			dataSetlist.stream().forEach(item->{ newList.add(item.changeClass()); });
			newinfo.setDataSetlist(newList);
		}
		if(paraList!=null && !paraList.isEmpty()){
			List<DeployDataparams> newList = new ArrayList<DeployDataparams>();
			paraList.stream().forEach(item->{ newList.add(item.changeClass()); });
			newinfo.setParaList(newList);
		}
		if(refList!=null && !refList.isEmpty()){
			List<DeployReferenceData> newList = new ArrayList<DeployReferenceData>();
			refList.stream().forEach(item->{ newList.add(item.changeClass()); });
			newinfo.setRefList(newList);
		}
		if(refModelList!=null && !refModelList.isEmpty()){
			List<com.tansun.ruledata.entity.RuleVersioninfo> newList = new ArrayList<com.tansun.ruledata.entity.RuleVersioninfo>();
			refModelList.stream().forEach(item->{ newList.add(item.changeClass()); });
			newinfo.setRefModelList(newList);
		}
		return newinfo;
	}
	public RuleDeployment getDeployment() {
		return deployment;
	}
	public void setDeployment(RuleDeployment deployment) {
		this.deployment = deployment;
	}
	public List<RuleEnterpoint> getEnterPoints() {
		return enterPoints;
	}
	public void setEnterPoints(List<RuleEnterpoint> enterPoints) {
		this.enterPoints = enterPoints;
	}
	public List<RuleVersioninfo> getFlowVersions() {
		return flowVersions;
	}
	public void setFlowVersions(List<RuleVersioninfo> flowVersions) {
		this.flowVersions = flowVersions;
	}
	public List<RuleDataset> getDataSetlist() {
		return dataSetlist;
	}
	public void setDataSetlist(List<RuleDataset> dataSetlist) {
		this.dataSetlist = dataSetlist;
	}
	public List<RuleDataparams> getParaList() {
		return paraList;
	}
	public void setParaList(List<RuleDataparams> paraList) {
		this.paraList = paraList;
	}
	public List<RuleReferenceData> getRefList() {
		return refList;
	}
	public void setRefList(List<RuleReferenceData> refList) {
		this.refList = refList;
	}
	
	public List<RuleVersioninfo> getRefModelList() {
		return refModelList;
	}
	public void setRefModelList(List<RuleVersioninfo> refModelList) {
		this.refModelList = refModelList;
	}
	/**
	 * 获取该实体的sql脚本
	 * @return
	 */
	public String getDeploySql() {
		List<String> sqlList = new ArrayList<>();
		String timeStamp = String.valueOf(new Date().getTime());
		StringBuffer sf = new StringBuffer();
		/****声明插入时间*****/
//		sf.append("set @currDate = SYSDATE();\n\r");//mysql
		String currDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		/****数据集脚本*****/
		String dsSql = "";
		for (RuleDataset ds : this.getDataSetlist()) {
			if(StringUtils.isEmpty(dsSql)) {
				dsSql = ds.getSql();
				sf.append(dsSql);
			}
			sqlList.add(ds.getArgs(timeStamp,currDate));
		}
		sf.append(sqlList.toString().substring(1,sqlList.toString().length()-1)).append("@;@\n\r");
		/****数据变量脚本*****/
		String paramSql = "";
		sqlList = new ArrayList<>();
		for (RuleDataparams param : this.getParaList()) {
			if(StringUtils.isEmpty(paramSql)) {
				paramSql = param.getSql();
				sf.append(paramSql);
			}
			sqlList.add(param.getArgs(timeStamp,currDate));
		}
		sf.append(sqlList.toString().substring(1,sqlList.toString().length()-1)).append("@;@\n\r");
		/****部署信息脚本*****/
		sf.append(this.getDeployment().getSql()).append(this.getDeployment().getArgs(timeStamp,currDate)).append("@;@\n\r");
		String enterpointSql = "";
		sqlList = new ArrayList<>();
		/****入口点信息脚本*****/
		for(RuleEnterpoint enterpoint : this.getEnterPoints()) {
			if(StringUtils.isEmpty(enterpointSql)) {
				enterpointSql = enterpoint.getSql();
				sf.append(enterpointSql);
			}
			sqlList.add(enterpoint.getArgs(timeStamp,currDate));
		}
		sf.append(sqlList.toString().substring(1,sqlList.toString().length()-1)).append("@;@\n\r");
		/****规则引用变量脚本*****/
		String refSql = "";
		sqlList = new ArrayList<>();
		for(RuleReferenceData ref : this.getRefList()) {
			if(StringUtils.isEmpty(refSql)) {
				refSql = ref.getSql();
				sf.append(refSql);
			}
			sqlList.add(ref.getArgs(timeStamp,currDate));
		}
		sf.append(sqlList.toString().substring(1,sqlList.toString().length()-1)).append("@;@\n\r");
		/****规则版本信息脚本*****/
		String versionSql = "";
		sqlList = new ArrayList<>();
		for(RuleVersioninfo ruleVersion : this.getFlowVersions()) {
			if(StringUtils.isEmpty(versionSql)) {
				versionSql = ruleVersion.getSql();
				sf.append(versionSql);
			}
			if(ruleVersion.getRuleContent()!=null) {
				String ruleContent = ruleVersion.getRuleContent().replaceAll("\'", "\\\\'");
				ruleVersion.setRuleContent(ruleContent);
			}
			sqlList.add(ruleVersion.getArgs(timeStamp,currDate));
		}
		sf.append(sqlList.toString().substring(1,sqlList.toString().length()-1)).append("@;@\n\r");
		
//		String refversionSql = "";
//		sqlList = new ArrayList<>();
//		for(RuleVersioninfo ruleVersion : this.getRefModelList()) {
//			if(StringUtils.isEmpty(refversionSql)) {
//				refversionSql = ruleVersion.getSql();
//				sf.append(refversionSql);
//			}
//			sqlList.add(ruleVersion.getArgs(timeStamp,currDate));
//		}
//		sf.append(sqlList.toString().substring(1,sqlList.toString().length()-1)).append("@;@\n\r");
		return sf.toString();
	}
}